package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysMenu;
import com.csyf.manager.entity.SysRole;
import com.csyf.manager.service.SysMenuService;
import com.csyf.manager.service.SysRoleService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.vo.MenuTreeListVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: casper
 * @Description: 角色管理
 * @Date: Created in 2018/11/23 16:02
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 角色列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public JqGridVo list(SysRole vo, Integer page, Integer rows) {
        return sysRoleService.queryRoleList(vo, page, rows);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultEntity addRole(SysRole role) {
        try {
            int num = sysRoleService.addRole(role);
            if (num > 0 ) {
                return ResultUtli.DEFAULT_SUCCESS("添加角色成功",null);
            } else {
                return ResultUtli.DEFAULT_ERROR("添加角色失败");
            }
        } catch (Exception e) {
            return ResultUtli.DEFAULT_ERROR("添加角色失败");
        }
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultEntity deleteRole(String roleId) {
        List<String> roleIdsList = Arrays.asList(roleId);
        int num = sysRoleService.updateStatusBatch(roleIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除角色成功！", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除角色失败");
        }
    }

    /**
     * 批量删除角色
     * @param roleIdStr
     * @return
     */
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResultEntity batchDeleteRole(String roleIdStr) {
        String[] roleIds = roleIdStr.split(",");
        List<String> roleIdsList = Arrays.asList(roleIds);
        int num = sysRoleService.updateStatusBatch(roleIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除角色成功！", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除角色失败");
        }
    }

    /**
     * 编辑角色详情
     * @param model
     * @param roleId
     * @return
     */
    @GetMapping("/edit")
    public String detail(Model model, Long roleId) {
        SysRole role = sysRoleService.findById(roleId);
        model.addAttribute("role", role);
        return "role/detail";
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity editRole(@ModelAttribute("role") SysRole role) {
        int num = sysRoleService.updateByRoleId(role);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑角色成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑角色失败");
        }
    }

    /**
     * 分配权限菜单列表查询
     * @param roleId
     * @return
     */
    @PostMapping("/assign/menu/list")
    @ResponseBody
    public List<MenuTreeListVo> assignRole(String roleId) {
        List<MenuTreeListVo> listVos = new ArrayList<>();
        List<SysMenu> allPermissions = sysMenuService.selectAll(CommonConst.STATUS_VALID);
        List<SysMenu> hasPermissions = sysMenuService.findMenusByRoleId(roleId);

        for (SysMenu menu : allPermissions) {
            MenuTreeListVo vo = new MenuTreeListVo();
            vo.setId(menu.getId());
            vo.setMenuId(menu.getMenuId());
            vo.setName(menu.getName());
            vo.setParentId(menu.getParentId());

            for (SysMenu hasMenu : hasPermissions) {
                //有权限则选中
                if(hasMenu.getMenuId().equals(menu.getMenuId())){
                    vo.setChecked(true);
                    break;
                }
            }
            listVos.add(vo);
        }
        return listVos;
    }

    /**
     * 分配菜单
     * @param roleId
     * @param menuIdStr
     * @return
     */
    @PostMapping("/assign/menu")
    @ResponseBody
    public ResultEntity assignRole(Long roleId, String menuIdStr) {
        List<String> menuIdsList = new ArrayList<>();
        if (StringUtils.isNotBlank(menuIdStr)) {
            String[] menuIds = menuIdStr.split(",");
            menuIdsList = Arrays.asList(menuIds);
        }

        ResultEntity resultEntity = sysRoleService.addAssignMenu(roleId, menuIdsList);
        return resultEntity;
    }






}
